import { createSlice } from '@reduxjs/toolkit'
import uuid from 'uuid'

// Define the initial state using that type
const initialState = {
  list: [],
  ready: false
}

export const pagesSlice = createSlice({
  name: 'pages',
  initialState,
  reducers: {
    push: (state, action) => {
      const d = action.payload
      d.id = uuid()
      state.list.push(d)
    },
    pop: (state) => {
      if (state.list.length > 1) {
        state.list.pop()
      } else {
        state.list = [{ element: 'admin', title: '首页', id: uuid() }]
      }
    },
    replace: (state, action) => {
      const d = action.payload
      d.id = uuid()
      state.list.splice(state.list.length - 1, 1, d)
    },
    setList: (state, action) => {
      state.list = action.payload
    },
    setReady: (state, action) => {
      state.ready = action.payload
    }
  },
})

export const { push, pop, setList, setReady, replace } = pagesSlice.actions

export default pagesSlice.reducer